package com.sjy.ylxt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sjy.ylxt.entity.basic.ResultBody;
import com.sjy.ylxt.entity.dto.QueryReportsDTO;
import com.sjy.ylxt.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.sjy.ylxt.entity.domain.YlxtReports;
import com.sjy.ylxt.mapper.YlxtReportsMapper;
import com.sjy.ylxt.service.YlxtReportsService;

import javax.annotation.Resource;
import java.util.List;

/**
* @Author: 史俊颖
* @CreateTime: 2024-12-12
* @Description: ${description}
*/
@Service
@Slf4j
public class YlxtReportsServiceImpl extends ServiceImpl<YlxtReportsMapper, YlxtReports>  implements YlxtReportsService{

    @Resource
    private YlxtReportsMapper ylxtReportsMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return ylxtReportsMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(YlxtReports record) {
        return ylxtReportsMapper.insert(record);
    }

    @Override
    public int insertSelective(YlxtReports record) {
        return ylxtReportsMapper.insertSelective(record);
    }

    @Override
    public YlxtReports selectByPrimaryKey(Integer id) {
        return ylxtReportsMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(YlxtReports record) {
        return ylxtReportsMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(YlxtReports record) {
        return ylxtReportsMapper.updateByPrimaryKey(record);
    }

    /**
     * 报告分页查询列表
     * @param dto
     * @return
     */
    @Override
    public ResultBody<?> queryReports(QueryReportsDTO dto) {
        try {
            log.info("==> 报告分页查询列表");
            PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
            LambdaQueryWrapper<YlxtReports> qw = new LambdaQueryWrapper<>();
            qw.eq(StringUtils.isNotEmpty(dto.getCreateUser()), YlxtReports::getCreateUser, dto.getCreateUser());
            qw.eq(StringUtils.isNotEmpty(dto.getUpdateUser()), YlxtReports::getUpdateUser, dto.getUpdateUser());
            qw.eq(dto.getReportType() != null, YlxtReports::getReportType, dto.getReportType());
            qw.eq(dto.getPatientCount() != null, YlxtReports::getPatientCount, dto.getPatientCount());
            qw.eq(dto.getAppointmentCount() != null, YlxtReports::getAppointmentCount, dto.getAppointmentCount());
            qw.eq(dto.getCompletedAppointmentCount() != null, YlxtReports::getCompletedAppointmentCount, dto.getCompletedAppointmentCount());
            qw.eq(dto.getCancelledAppointmentCount() != null, YlxtReports::getCancelledAppointmentCount, dto.getCancelledAppointmentCount());
            qw.eq(dto.getAverageEvaluationScore() != null, YlxtReports::getAverageEvaluationScore, dto.getAverageEvaluationScore());
            qw.eq(dto.getDeleteFlag() != null, YlxtReports::getDeleteFlag, dto.getDeleteFlag());
            qw.between(dto.getReportDateStart() != null && dto.getReportDateEnd() != null, YlxtReports::getReportDate, dto.getReportDateStart(), dto.getReportDateEnd());
            qw.orderByDesc(YlxtReports::getCreateTime);
            List<YlxtReports> ylxtReports = ylxtReportsMapper.selectList(qw);
            PageInfo<YlxtReports> pageInfo = new PageInfo<>(ylxtReports);
            return ResultBody.success(pageInfo);
        } catch (Exception e) {
            log.error("==> 报告分页查询列表失败,报错信息为{}", e.getMessage());
            return ResultBody.error("查询报告列表失败");
        }
    }

}
